import Vue from 'vue'
import Vuex from 'vuex'
import {
  login,
  forgetPwd,
  logout,
  getInfo
} from '@/api/login'
import {
  getToken,
  setToken,
  removeToken,
  setUserInfo,
  getUserInfo
} from '@/utils/auth'
import {
  routes
} from '@/router'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: getToken(),
    tagsList: [],
    collapse: false,
    // jurisdiction: '12000,11100,11200,11300,11400,11500,11600,11700,11800,11900,12000',  //权限
    jurisdiction: 'ALL', //权限
    userInfo: getUserInfo(),
    init: false, // 是否完成初始化 // 默认未完成
    RouterList: [] // 动态路由
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
      console.log('登录成功-userInfo--', userInfo);
      console.log('登录成功-本地--userInfo--', getUserInfo());
    },
    getTags(state, data) {
      state.tagsList = data;
    },
    delTagsItem(state, data) {
      state.tagsList.splice(data.index, 1);
      sessionStorage.setItem('tagsList', JSON.stringify(state.tagsList))
    },
    setTagsItem(state, data) {
      state.tagsList.push(data)
      sessionStorage.setItem('tagsList', JSON.stringify(state.tagsList))
    },
    clearTags(state) {
      state.tagsList = []
      sessionStorage.setItem('tagsList', JSON.stringify(state.tagsList))
    },
    closeTagsOther(state, data) {
      state.tagsList = data;
      sessionStorage.setItem('tagsList', JSON.stringify(state.tagsList))
    },
    closeCurrentTag(state, data) {
      for (let i = 0, len = state.tagsList.length; i < len; i++) {
        const item = state.tagsList[i];
        if (item.path === data.$route.fullPath) {
          if (i < len - 1) {
            data.$router.push(state.tagsList[i + 1].path);
          } else if (i > 0) {
            data.$router.push(state.tagsList[i - 1].path);
          } else {
            data.$router.push("/");
          }
          state.tagsList.splice(i, 1);
          sessionStorage.setItem('tagsList', JSON.stringify(state.tagsList))
          break;
        }
      }
    },
    // 侧边栏折叠
    handleCollapse(state, data) {
      state.collapse = data;
    },
    set_router: (state, RouterList) => {
      state.RouterList = RouterList
    },
    set_init: (state, status) => {
      state.init = status
    }
  },
  actions: {
    // 动态设置路由 此为设置设置途径
    setRouterList({
      commit
    }, routerList) {
      // console.log('routes--',routes)
      commit('set_router', routes.concat(routerList)) // 进行路由拼接并存储
    },
    // 登录
    Login({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(res => {
          // setToken(res.token)
          // commit('SET_TOKEN', res.token)
          setUserInfo(JSON.stringify(res.result))
          commit('SET_USERINFO', res.result)
          console.log('登录成功---', res);
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 修改密码
    RepairPassword({
      commit
    }, userInfo) {
      const username = userInfo.username
      const password = userInfo.password
      const code = userInfo.code
      return new Promise((resolve, reject) => {
        forgetPwd(username, password, code).then(res => {
          //setToken(res.token)
          // commit('SET_TOKEN', res.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 退出系统
    LogOut({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        resolve()
        /* logout(state.token).then(() => {
          commit('SET_TOKEN', '')
          // commit('SET_ROLES', [])
          // commit('SET_PERMISSIONS', [])
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        }) */
      })
    },
  },
  modules: {}
})